package com.hrh.selectsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrh.selectsystem.aop.MyJwtInterceptor;
import com.hrh.selectsystem.common.ErrorCode;
import com.hrh.selectsystem.exception.BusinessException;
import com.hrh.selectsystem.model.entity.Admin;
import com.hrh.selectsystem.model.entity.Student;
import com.hrh.selectsystem.model.vo.admin.AdminVO;
import com.hrh.selectsystem.model.vo.admin.LoginAdminVO;
import com.hrh.selectsystem.service.AdminService;
import com.hrh.selectsystem.mapper.AdminMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.hrh.selectsystem.constant.UserConstant.USER_LOGIN_STATE;

/**
* @author hetongxue
* @description 针对表【admin(管理员)】的数据库操作Service实现
* @createDate 2024-10-23 11:23:21
*/
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService{

    /**
     * 盐值，混淆密码
     */
    //public static final String SALT = "hrh";
    @Resource
    private AdminMapper adminMapper;

    /**
     * 管理员登录
     *
     * @param adminName
     * @param adminPassword
     * @param request
     * @return
     */
    @Override
    public LoginAdminVO adminLogin(String adminName, String adminPassword, HttpServletRequest request) {
        // 1. 校验
        if (StringUtils.isAnyBlank(adminName, adminPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        // 2. 加密
        //String encryptPassword = DigestUtils.md5DigestAsHex((SALT + adminPassword).getBytes());
        // 查询用户是否存在
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminName", adminName);
        queryWrapper.eq("adminPassword", adminPassword);
        Admin admin = this.baseMapper.selectOne(queryWrapper);
        // 用户不存在
        if (admin == null) {
            log.info("admin login failed, adminAccount cannot match adminPassword");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
        }
        // 3. 记录用户的登录态
        //request.getSession().setAttribute(USER_LOGIN_STATE, admin);
        return this.getLoginAdminVO(admin);
    }

    /**
     * 管理员注销
     * @param request
     * @return
     */
    @Override
    public boolean adminLogout(HttpServletRequest request) {
        if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "未登录");
        }
        // 移除登录态
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        return true;
    }

    /**
     * 获取当前登录管理员
     * @param request
     * @return
     */
    @Override
    public Admin getLoginAdmin(HttpServletRequest request) {
        // 从请求头中获取token
        String token = request.getHeader("token");
        // 获取用户id
        String userId = MyJwtInterceptor.getUserIdFromToken(token);
        // 查询用户
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return admin;
    }

    /**
     * 获取封装后用户信息(脱敏 & 登录后返回)
     * @param admin
     * @return
     */
    private LoginAdminVO getLoginAdminVO(Admin admin) {
        if (admin == null) {
            return null;
        }
        LoginAdminVO loginAdminVO = new LoginAdminVO();
        BeanUtils.copyProperties(admin, loginAdminVO);
        return loginAdminVO;
    }

    /**
     * 获取封装后用户信息（脱敏）
     * @param admin
     * @return
     */
    public AdminVO getAdminVO(Admin admin) {
        if (admin == null) {
            return null;
        }
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(admin, adminVO);
        return adminVO;
    }

}




